#from flask import Flask,render_template
# app = Flask(__name__)
# @app.route('/')
# def index():
#     return '<h1>Hello World!</h1>'

# @app.route('/user/<name>')
# def user(name):
#     return '<h1>Hello, %s!</h1>' % name

# @app.route('/hello')
# def test():
#     return render_template('index.html')

# if __name__ == '__main__':
#     app.run(debug=True)



import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'data.sqlite')
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True

db = SQLAlchemy(app)


class Role(db.Model):
    __tablename__ = 'roles'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)
    users = db.relationship('User', backref='role')
    def __repr__(self):
        return '<Role %r>' % self.name

class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), unique=True, index=True)
    role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))
    def __repr__(self):
        return '<User %r>' % self.username



from flask_script import Shell,Manager
manager = Manager(app)

# 每次启动一个shell会话都不得不导入数据库实例和模型，相当繁琐。为了解决每次的这些重复导入，
# Flask-Script shell命令行可以用来配置自动导入相关对象。
# 为了把对象添加到导入列表，shell命令行需要被注册为make_context回调函数
def make_shell_context():
    return dict(app=app, db=db, User=User, Role=Role)
manager.add_command("shell", Shell(make_context=make_shell_context))

#用flask_script扩展里面的Manager启动项目，
#用python test_pro、hello.py shell启动项目时，必须要有下面代码
if __name__ == '__main__':
     manager.run()